package cn.bdqn.controller;

import cn.bdqn.domain.Client;
import cn.bdqn.service.ClientService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 王浩
 * @create 2020/9/19 10:01
 */
@RestController
public class ClientController {

    @Autowired
    private ClientService service;

    /**
     * 添加客户信息
     * @param client
     */
    @PostMapping("/client/addClientInfo")
    public Result AddClientInfo(@RequestBody Client client){
        try{
            service.AddClientInfo(client);
            //返回结果
            return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("添加失败");
        }
    }

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/client/queryInfoById/{id}")
    public Result queryInfoById(@PathVariable("id")Integer id) {
        try{
            //返回结果
            return  ResultGenerator.genSuccessResult(service.queryInfoById(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 根据手机号查询客户信息
     * @param phone
     * @return
     */
    @GetMapping("/client/queryInfoByPhone/{phone}")
    public Result queryInfoByPhone(@PathVariable("phone") String phone) {
        try{
            //返回结果
            return  ResultGenerator.genSuccessResult(service.queryInfoByPhone(phone));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 根据手机号判断手机号是否已注册
     *
     * @param phone 电话
     * @return
     */
    @GetMapping("/client/queryByPhoneIsExist/{phone}")
    public Result queryByPhoneIsExist(@PathVariable("phone")String phone) {
        try{
            //返回结果
            return  ResultGenerator.genSuccessResult(service.queryByPhoneIsExist(phone));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 查询全部并分页
     *
     * @param pageNum   当前页码
     * @param pageAmount    每页条数
     * @return
     */
    @GetMapping("/client/queryAllInfo/{pageNum}/{pageAmount}")
    public Result queryAllInfo(@PathVariable("pageNum")Integer pageNum, @PathVariable("pageAmount")Integer pageAmount) {
        try{
            Page page = service.queryAllInfo(pageNum,pageAmount);
            //返回结果
            return  ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 根据电话或姓名模糊查询信息并分页
     *
     * @param phone  客户电话
     * @param name   客户姓名
     * @param pageNum  当前页码
     * @param pageAmount  每页条数
     * @return
     */
    @GetMapping("/client/queryInfoByPhoneAndName/{phone}/{name}/{pageNum}/{pageAmount}")
    public Result queryInfoByPhoneAndName(@PathVariable("phone")String phone,
                                          @PathVariable("name") String name,
                                          @PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageAmount") Integer pageAmount) {
        System.out.println(name);
        try{
            Page page = service.queryInfoByPhoneAndName(phone.trim(),name.trim(),pageNum,pageAmount);
            //返回结果
            return  ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 逻辑删除
     * @param id 主键id
     */
    @PutMapping("/client/alterStateById/{id}")
    public Result alterStateById(@PathVariable("id")Integer id) {
        try{
            service.alterStateById(id);
            return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("修改失败");
        }
    }

    /**
     * 修改客户信息
     *
     * @param client
     */
    @PutMapping("/client/alterInfo")
    public Result alterInfo(@RequestBody Client client) {
        try{
            service.alterInfo(client);
            //返回结果
            return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("更新失败");
        }
    }

}
